package com.osdp.sc.controller;

import com.alibaba.fastjson.JSONObject;
import com.osdp.sc.service.IScRolePermissionService;
import com.osdp.sc.vo.ScRolePermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色授权管理")
@RestController
//@RefreshScope
@RequestMapping("/sc/rolePermission")
public class ScRolePermissionController {

    @Autowired
    private IScRolePermissionService rolePermissionService;

    @ApiOperation("根据角色ID获取角色权限")
    @GetMapping("/queryRolePermsByRoleId")
    public List<ScRolePermissionVo> queryRolePermsByRoleId(@RequestParam(name = "roleId", required = true) String roleId){
        return rolePermissionService.queryRolePermsByRoleId(roleId);
    }

    @RequestMapping(value = "/saveRolePermission", method = RequestMethod.POST)
    @ApiOperation("保存角色权限")
    public boolean saveRolePermission(@RequestBody JSONObject jsonObject){
        String roleId = jsonObject.getString("roleId");
        List<String> permissionIds = null;
        if(jsonObject.containsKey("permissionIds")) {
            permissionIds = jsonObject.getJSONArray("permissionIds").toJavaList(String.class);
        }
        return rolePermissionService.saveRolePermission(roleId,permissionIds);
    }
}
